<template>
  <div class="app-container" style="height: 100%">
    <el-form ref="queryForm" :model="queryParams" size="mini" inline style="white-space: nowrap; overflow-x: auto">
      <el-form-item prop="constructionCode">
        <el-input v-model.trim="queryParams.constructionCode" placeholder="请输入社会信用代码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item prop="constructionName">
        <el-input v-model.trim="queryParams.constructionName" placeholder="请输入公司名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button size="mini" type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button size="mini" icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        <el-button size="mini" type="primary" icon="el-icon-plus" @click="showEditItem = true" v-hasPermi="['baseManage:rdcConstruction:add']">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="list" size="small" height="calc(100% - 100px)">
      <el-table-column label="社会信用代码" prop="constructionCode" show-overflow-tooltip min-width="100" />
      <el-table-column label="公司名称" prop="constructionName" show-overflow-tooltip min-width="150" />
      <el-table-column label="公司地址" prop="address" show-overflow-tooltip min-width="100" />
      <el-table-column label="公司电话" prop="constructionTel" show-overflow-tooltip min-width="80" />
      <el-table-column label="负责人名称" prop="principalName" show-overflow-tooltip min-width="80" />
      <el-table-column label="负责人电话" prop="principalTel" show-overflow-tooltip min-width="80" />
      <el-table-column label="修改时间" prop="updateTime" align="center" width="140" />
      <el-table-column label="创建时间" prop="createTime" align="center" width="140" />
      <el-table-column label="操作" align="center" width="120" fixed="right">
        <template slot-scope="{ row }">
          <el-button size="mini" type="text" icon="el-icon-edit" :disabled="row.removeLoading" @click="$refs.EditItem.open(row)" v-hasPermi="['baseManage:rdcConstruction:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" :loading="row.removeLoading" @click="handleDelete(row)" v-hasPermi="['baseManage:rdcConstruction:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <EditItem ref="EditItem" v-model="showEditItem" @ok="getList" />
  </div>
</template>
<script>
// 工程质量-施工/承建单位
import { pageRdcConstruction, delRdcConstruction } from '@/api/qualityManage/cq'
import EditItem from './components/EditRdcConstruction'
export default {
  name: 'RdcConstruction',
  components: { EditItem },
  data() {
    return {
      loading: false,
      total: 0,
      list: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        constructionCode: undefined,
        constructionName: undefined
      },
      showEditItem: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      this.loading = true
      const res = await pageRdcConstruction(this.queryParams).catch(() => false)
      this.loading = false
      if (res === false) return

      const data = res.rows || []
      data.map(i => {
        i.removeLoading = false
      })
      this.list = data
      this.total = res.total || 0
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    async handleDelete(item) {
      if (item.removeLoading) return
      const res1 = await this.$confirm(`确认删除：${item.constructionName}?`, '警告', { type: 'warning' }).catch(() => false)
      if (res1 !== 'confirm') return

      item.removeLoading = true
      const res = await delRdcConstruction(item.constructionId).catch(() => false)
      item.removeLoading = false
      if (res === false) return

      this.msgSuccess('删除成功')
      this.list.length > 1 ? this.getList() : this.handleQuery()
    }
  }
}
</script>
